package com.runtime.hongyunuser.controller;

import com.alibaba.fastjson.JSONObject;
import com.runtime.hongyunuser.bean.*;
import com.runtime.hongyunuser.bean.vo.CartProductVO;
import com.runtime.hongyunuser.bean.vo.ProductsVo;
import com.runtime.hongyunuser.service.ProductService;
import com.runtime.hongyunuser.service.ShoppingcartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("shoppingcarts")
public class ShoppingcartsController {

    @Autowired
    private ShoppingcartsService shoppingcartsService;

    @Autowired
    private ProductService productService;


//    @GetMapping("/product/{productId}")
//    public Result getProductById(@PathVariable long productId) {
//        ProductsVo product = productService.getProductById(productId);
//        return new Result(Code.GET_OK, product, "获取成功");
//    }

    // 添加商品到购物车
    @PostMapping("/add")
    public Result addToCart(@RequestBody Map<String,Long> shoppingcarts) {
        try {
            Long userId=shoppingcarts.get("userId");
            Long productId=shoppingcarts.get("productId");
            Long quantity=shoppingcarts.get("quantity");
            Shoppingcarts cartItem = shoppingcartsService.addToCart(userId, productId, quantity);
            return new Result(Code.SAVE_OK, cartItem, "添加成功");
        } catch (Exception e) {
            return new Result(Code.SAVE_ERR, null, e.getMessage());
        }
    }

    // 获取用户购物车
    @GetMapping("/items")
    public Result getCartItems(@RequestParam Long userId) {
        List<CartProductVO> resultList = shoppingcartsService.getCartProductsByUserId(userId);
        return new Result(Code.GET_OK, resultList, "获取成功");
    }

    // 更新购物车项数量
    @PostMapping("/update")
    public Result updateQuantity(@RequestParam Long cartId,
                                 @RequestParam Integer quantity) {
        try {
            shoppingcartsService.updateQuantity(cartId, quantity);
            return new Result(Code.UPDATE_OK, null, "更新成功");
        } catch (Exception e) {
            return new Result(Code.UPDATE_ERR, null, e.getMessage());
        }
    }

    // 从购物车删除商品
    @DeleteMapping("/remove")
    public Result removeFromCart(@RequestParam Long userId,  @RequestParam Long productId ) {
        try {
            shoppingcartsService.removeFromCart(userId, productId);
            return new Result(Code.DELETE_OK, null, "删除成功");
        } catch (Exception e) {
            return new Result(Code.DELETE_ERR, null, e.getMessage());
        }
    }

    @DeleteMapping("/removeBatch")
    public Result removeBatchFromCart(
            @RequestParam List<Long> cartIds,
            HttpServletRequest request) {

        // 1. 从Session获取登录用户
        Users loginUser = (Users) request.getSession().getAttribute("loginAcc");
        if (loginUser == null) {
            return new Result(Code.SYSTEM_ERR, null, "请先登录");
        }

        // 2. 参数基础验证
        if (cartIds == null || cartIds.isEmpty()) {
            return new Result(Code.SYSTEM_ERR, null, "请选择要删除的商品");
        }

        try {
            // 3. 调用Service执行删除
            int deletedCount = shoppingcartsService.removeBatchFromCart(
                    loginUser.getUserId(),
                    cartIds
            );

            // 4. 返回结果
            String message = deletedCount > 0
                    ? String.format("成功删除%d条购物车记录", deletedCount)
                    : "没有记录被删除";

            return new Result(Code.DELETE_OK, deletedCount, message);

        }catch (Exception e) {
            return new Result(Code.SYSTEM_ERR, null, "系统繁忙，请稍后重试");
        }
    }
}